package com.zhilifang.modules.zlf.controller;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zhilifang.modules.zlf.entity.Seo;
import com.zhilifang.modules.zlf.entity.SeoLog;
import com.zhilifang.modules.zlf.service.impl.SeoLogServiceImpl;
import com.zhilifang.modules.zlf.service.impl.SeoServiceImpl;
import com.zhilifang.modules.zlf.utils.Pager;
import com.zhilifang.modules.zlf.utils.QueryString;
import com.zhilifang.modules.zlf.utils.Result;
import com.zhilifang.modules.zlf.utils.Sitemap;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

/**
 * Controller 后台首页>>SEO优化Controller
 */
@Controller
@RequestMapping("/seoController")
public class SeoController {

	@Autowired
	private SeoServiceImpl seoServiceImpl;
	
	@Autowired
	private SeoLogServiceImpl seoLogServiceImpl;
	
	@Autowired
    private Configuration configuration;  		//模板
	
	
	/**
	 * Seo列表加载
	 * 
	 * @return
	 */
	@RequestMapping(value = "/selectSeoList", method = RequestMethod.POST)
	@ResponseBody
	public List<Seo> selectSeoList(){
		List<Seo> list = seoServiceImpl.selectSeoList();	
		return list;
	}
	
	

	/**
	 * seo修改修改
	 * seo日志添加记录
	 * 
	 * @return
	 */
	@RequestMapping(value = "/updateSeo", method = RequestMethod.POST)
	@ResponseBody
	public Result insertUpdateBanner(Seo seo){
		Result result = new Result();
		seoServiceImpl.updateSeo(seo);
		SeoLog seoLog = new SeoLog();
		if(seo.getName()!=null&&!"".equals(seo.getName())){
			seoLog.setName(seo.getName());
		}
		if(seo.getRemarks()!=null&&!"".equals(seo.getRemarks())){
			seoLog.setRemarks(seo.getRemarks());
		}
		if(seo.getTitle()!=null&&!"".equals(seo.getTitle())){
			seoLog.setTitle(seo.getTitle());
		}
		if(seo.getDescribe()!=null&&!"".equals(seo.getDescribe())){
			seoLog.setDescribe(seo.getDescribe());
		}
		if(seo.getKeyword()!=null&&!"".equals(seo.getKeyword())){
			seoLog.setKeyword(seo.getKeyword());
		}
		if(seo.getUpdate()!=null&&!"".equals(seo.getUpdate())){
			seoLog.setUpdate(seo.getUpdate());
		}
		if(seo.getId()!=null&&!"".equals(seo.getId())){
			seoLog.setSeoId(seo.getId());
		}
		if(seo.getSort()!=null&&!"".equals(seo.getSort())){
			seoLog.setSort(seo.getSort());
		}
		seoLogServiceImpl.insertSeoLog(seoLog);
		result.setMessage("修改成功");
		result.setState("200");
		return result;
	}
	
	
	/**
	 * 全部修改记录分页查询
	 * 
	 * @return
	 */
	@RequestMapping(value = "/selectPagerListQueryString", method = RequestMethod.POST)
	@ResponseBody
	public Pager<SeoLog> selectProductPageQueryString(QueryString queryString){
		Pager<SeoLog> pager = new Pager<SeoLog>();
		if(!"".equals(queryString.getReady01())&&queryString.getReady01()!=null){
			queryString.setReady01(queryString.getReady01());  //查询字段 1.标题 2.关键字 3.描叙
		}
		if(!"".equals(queryString.getReady02())&&queryString.getReady02()!=null){
			queryString.setReady02("%"+queryString.getReady02()+"%");  //查询内容
		}
		List<SeoLog> list = seoLogServiceImpl.selectPagerListQueryString(queryString);
		if (list != null&&!list.isEmpty()) {
			pager=new Pager<SeoLog>(queryString.getCurrentPage(),queryString.getPageSize(), list);
		}
		return pager;
	}
	
	
	
	/**
	 * 删除&批量删除SEO日志
	 * 
	 * @return
	 */
	@RequestMapping(value = "/deleteSeoLog", method = RequestMethod.POST)
	@ResponseBody
	public Result deleteSeoLog(int [] ids){
		Result result = new Result();
		seoLogServiceImpl.deleteSeoLog(ids);	
		result.setMessage("删除成功");
		result.setState("200");
		return result;
	}
	
	
	/**
	 * 后台网站地图--生成xml地图
	 * 
	 * @return
	 */
	@RequestMapping(value = "/createXML", method = RequestMethod.POST)
	@ResponseBody
	public Result createXML(){
		Result result = new Result();
		 try {
			String fileAdd = this.getClass().getResource("/").toURI().getPath()+"views/";
			Sitemap.createXML(fileAdd);
		} catch (URISyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 result.setMessage("XML地图生成成功");
		 result.setState("200");
		 return result;
	}
	
	/**
	 * 后台网站地图--生成html网页
	 * 
	 * @return
	 */
	@RequestMapping(value = "/createHTML", method = RequestMethod.POST)
	@ResponseBody
	public Result createHTML(){
		Result result = new Result();
		Map<String, Object> map = new HashMap<>();
		List<com.zhilifang.modules.zlf.entity.Sitemap>  list = Sitemap.createHTML();
		String str = "";
//		int i = 1;
//		for(com.zhilifang.modules.zlf.entity.Sitemap li:list){
//			if(!"".equals(li.getTitle())&&li.getTitle()!=""){
//				str+="<li class=\"lks\"><span>"+i+"</span><a href=\""+li.getLoc()+"\" title=\""+li.getTitle()+"\" target=\"_blank\">"+li.getTitle()+"</a></li>";
//				i++;
//			}
//		}
		map.put("str", str);
		createIndexHtml("sitemap.html", "sitemap.ftl", map);   //生成静态页面方法
		 result.setMessage("HTML网页生成成功");
		 result.setState("200");
		 return result;
	}
	
	
	
	/**
     * 生成首页静态页面
     * 生成文件名称、调用模板名称、存放数据
     * 
     */
    public void createIndexHtml(String fileName, String ftlName, Map<String, Object> map) {
        try {
            /**获取输出目标文件输出流------开始*/
            String filepath = this.getClass().getResource("/").toURI().getPath()+"views/reception/";
            System.out.println("每次访问新生成的html位置："+filepath);
            File folder = new File(filepath);
            //如果文件夹不存在
            if (!folder.exists()) {
                folder.mkdir();
            }
            File indexHtml = new File(folder, fileName);
            //如果html文件不存在
            if (!indexHtml.exists()) {
                indexHtml.createNewFile();
            }
            Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(indexHtml),"UTF-8"));
            /**获取输出目标文件输出流------结束*/
 
            //获取模板
            Template template = configuration.getTemplate(ftlName);
 
            //把数据和输出文件信息交给模板得到静态html文件
            template.process(map,out);
 
            out.flush();
            out.close();}
        catch (URISyntaxException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TemplateException e) {
            e.printStackTrace();
        }
    }
	
	
}
